package com.fjp.heartguard.ui.question.fragment

import android.os.Bundle
import android.widget.CompoundButton
import android.widget.CompoundButton.OnCheckedChangeListener
import com.fjp.heartguard.R
import com.fjp.heartguard.app.base.BaseFragment
import com.fjp.heartguard.databinding.FragmentCognitiveAssessment2Binding
import com.fjp.heartguard.ui.question.QuestionActivity
import com.fjp.heartguard.viewmodel.CognitiveAssessment2ViewModel
import com.github.gzuliyujiang.wheelpicker.DatePicker
import com.github.gzuliyujiang.wheelpicker.OptionPicker
import com.github.gzuliyujiang.wheelpicker.annotation.DateMode
import com.github.gzuliyujiang.wheelpicker.entity.DateEntity
import com.github.gzuliyujiang.wheelpicker.impl.UnitDateFormatter
import java.time.LocalDate
import java.util.Date


class CognitiveAssessment2Fragment : BaseFragment<CognitiveAssessment2ViewModel, FragmentCognitiveAssessment2Binding>() {
    val currentDate = LocalDate.now()
    val weekArr = arrayListOf("星期一", "星期二", "星期三", "星期四", "星期五", "星期六", "星期日")
    lateinit var monthAndDatePicker: DatePicker
    lateinit var yearPicker: OptionPicker
    lateinit var weekPicker: OptionPicker
    val yearArr = arrayListOf<Int>()
    override fun initView(savedInstanceState: Bundle?) {
        mDatabind.viewModel = mViewModel
        for (i in -15 until 16){
            yearArr.add(currentDate.year+i)
        }
        mDatabind.click = ClickProxy()
        mDatabind.questionTitle.setValue("认知功能评估（A）", resources.getColor(R.color.report_type_01), R.drawable.report_type_01)
        val date = Date()
        val dateEntity = DateEntity.target(date)

//        mViewModel.year.set(dateEntity.year.toString())
//        QuestionActivity.olderInfoQuestion4.select[0].value = mViewModel.year.get().toString()

//        mViewModel.monthAndDay.set("${currentDate.month.value}月${currentDate.dayOfMonth}日")
//        QuestionActivity.olderInfoQuestion5.select[0].value = "${currentDate.year}-${currentDate.month.value}-${currentDate.dayOfMonth}"

//        mViewModel.week.set(weekArr[LocalDate.now().dayOfWeek.value - 1])
//        QuestionActivity.olderInfoQuestion6.select[0].value = LocalDate.now().dayOfWeek.value.toString()
        mDatabind.yearCheckBox.setOnCheckedChangeListener { p0, p1 ->
            QuestionActivity.yearIsChecked = p1
            QuestionActivity.olderInfoQuestion4.select[0].unanswerable= if(p1) 1 else 0
            mDatabind.yearEt.isEnabled=!p1
        }
        mDatabind.monthCheckBox.setOnCheckedChangeListener { p0, p1 ->
            QuestionActivity.monthIsChecked=p1
            mDatabind.monthEt.isEnabled=!p1
            QuestionActivity.olderInfoQuestion5.select[0].unanswerable= if(p1) 1 else 0
        }
        mDatabind.weekCheckBox.setOnCheckedChangeListener { p0, p1 ->
            QuestionActivity.weekIsChecked = p1
            mDatabind.weekEt.isEnabled=!p1
            QuestionActivity.olderInfoQuestion6.select[0].unanswerable= if(p1) 1 else 0
        }

    }

    inner class ClickProxy {

        fun clickSelectYear() {
            if(QuestionActivity.yearIsChecked){
                return
            }
            showYearDialog()
        }

        fun clickSelectMonthAndDay() {
            if(QuestionActivity.monthIsChecked){
                return
            }
            showMonthDayDialog()
        }

        fun clickSelectWeek() {
            if(QuestionActivity.weekIsChecked){
                return
            }
            showWeekDialog()
        }

    }

    fun showMonthDayDialog() {
        activity?.let {
            val date = Date()
            if (!this@CognitiveAssessment2Fragment::monthAndDatePicker.isInitialized) {
                monthAndDatePicker = DatePicker(it)
                monthAndDatePicker.setBackgroundResource(R.drawable.bg_white_top_round_8)
                monthAndDatePicker.setTitle("选择日期")
                val wheelLayout = monthAndDatePicker.wheelLayout
                wheelLayout.setDateMode(DateMode.MONTH_DAY)
                wheelLayout.setDateLabel("", "", "")
                wheelLayout.setDateFormatter(UnitDateFormatter())
                wheelLayout.setRange(DateEntity.target(2010, 1, 1), DateEntity.target(currentDate.year + 10, currentDate.monthValue, currentDate.dayOfMonth), DateEntity.today())
                wheelLayout.setDefaultValue(DateEntity.target(date))
                wheelLayout.setCurtainEnabled(false)
                wheelLayout.setIndicatorEnabled(false)
                monthAndDatePicker.wheelLayout.setResetWhenLinkage(false)
                monthAndDatePicker.setOnDatePickedListener { year, month, day ->
                    mViewModel.monthAndDay.set("${month}月${day}日")
                    QuestionActivity.olderInfoQuestion5.select[0].value = "${currentDate.year}-${month}-${day}"
                }
            }
            monthAndDatePicker.show()
        }

    }

    fun showYearDialog() {
        activity?.let {
            yearPicker = OptionPicker(it)
            yearPicker.setBackgroundResource(R.drawable.bg_white_top_round_8)
            yearPicker.setTitle("选择年份")
            yearPicker.titleView.setTextColor(resources.getColor(R.color.black))
            yearPicker.setData(yearArr)
            yearPicker.setDefaultValue(mViewModel.year.get())
            yearPicker.setOnOptionPickedListener { position, item ->
                mViewModel.year.set(item.toString())
                QuestionActivity.olderInfoQuestion4.select[0].value = item.toString()
            }
            yearPicker.show()
        }
    }

    fun showWeekDialog() {
        activity?.let {
            weekPicker = OptionPicker(it)
            weekPicker.setBackgroundResource(R.drawable.bg_white_top_round_8)
            weekPicker.setTitle("选择星期")
            weekPicker.titleView.setTextColor(resources.getColor(R.color.black))
            weekPicker.setData(weekArr)
            weekPicker.setDefaultValue(mViewModel.week.get())
            weekPicker.setOnOptionPickedListener { position, item ->
                mViewModel.week.set(item.toString())
                QuestionActivity.olderInfoQuestion6.select[0].value = (position + 1).toString()
            }
            weekPicker.show()

        }
    }
}